###################################################
#           Replication file for                  #
# Thürk and Klüver: Hitting the Sweet Spot? (CPS) #
#             Panel Match Analysis                #
#          Models 81-88 in Tables 15-16           #
#             Figure 3 & Figures 8-9              #
###################################################

# Clean the workspace
rm(list = ls())

#load all required packages
library(tidyverse)
library(haven)
library(PanelMatch)
library(ggplot2)
library(ggpubr)
library(conflicted)
library(dplyr)

#====================================================================
#####################################################################
########## ANALYSIS FULL SAMPLE, for Figure 3, Models 81-84 #########
#####################################################################
#====================================================================

# Read Stata data
data <- read_dta(".../enlarged_data_ThuerkKluever.dta")

# Create data frame with variables of interest based on enlarged data set (we need annually data, not based on election year in order to match)
data2 <- 	data.frame(
  year=data$year,
  party=data$party,
  treat_support=data$treat_support,
  treat_support_formal=data$treat_support_formal,
  treat_support_subst=data$treat_support_subst,
  treat_junior=data$treat_junior,
  next_pervote=data$next_pervote,
  pervote=data$pervote,
  e_id=data$e_id
)
# Need to be integers 
data2$year <- as.integer(data$year)
data2$party <- as.integer(data$party)

######################################################################
####################### JUNIOR COALITION PARTY #######################
######################################################################
#### Model 81
PM.results_subs <- PanelMatch(lag = 1, time.id = "year", unit.id = "party", 
                              treatment = "treat_junior", refinement.method = "ps.match", 
                              data = data2, match.missing = TRUE, 
                              covs.formula = ~ I(lag(pervote, 1:2)), 
                              size.match = 3, qoi = "att" ,outcome.var = "next_pervote",
                              lead = 0:2, forbid.treatment.reversal = FALSE)

PE.results4 <- PanelEstimate(sets = PM.results_subs, data = data2)
#print results
PE.results4
#print plot
juniorplot <- plot(PE.results4, 
                   main="Junior coalition partner")

######################################################################
####################### ANY KIND OF SUPPORT ##########################
######################################################################
#### Model 82
PM.results_supp <- PanelMatch(lag = 1, time.id = "year", unit.id = "party", 
                              treatment = "treat_support", refinement.method = "ps.match", 
                              data = data2, match.missing = TRUE, 
                              covs.formula = ~ I(lag(pervote, 1:2)), 
                              size.match = 3, qoi = "att" ,outcome.var = "next_pervote",
                              lead = 0:2, forbid.treatment.reversal = FALSE)

PE.results3 <- PanelEstimate(sets = PM.results_supp, data = data2)
#print results
PE.results3
#print plot
supportplot <- plot(PE.results3, 
                    main="Any kind of support party")

######################################################################
####################### CONTRACT SUPPORT PARTY########################
######################################################################
#### Model 83
PM.results_formal <- PanelMatch(lag = 1, time.id = "year", unit.id = "party", 
                                treatment = "treat_support_formal", refinement.method = "ps.match", 
                                data = data2, match.missing = TRUE, 
                                covs.formula = ~ I(lag(pervote, 1:2)), 
                                size.match = 3, qoi = "att" ,outcome.var = "next_pervote",
                                lead = 0:2, forbid.treatment.reversal = FALSE)

PE.results2 <- PanelEstimate(sets = PM.results_formal, data = data2)
#print results
PE.results2
#print plot
contractplot <- plot(PE.results2, 
                     main="Contract support party")

######################################################################
####################### DECLARED SUPPORT PARTY########################
######################################################################
#### Model 84
PM.results_jun <- PanelMatch(lag = 1, time.id = "year", unit.id = "party", 
                             treatment = "treat_support_subst", refinement.method = "ps.match", 
                             data = data2, match.missing = TRUE, 
                             covs.formula = ~ I(lag(pervote, 1:2)), 
                             size.match = 3, qoi = "att" ,outcome.var = "next_pervote",
                             lead = 0:2, forbid.treatment.reversal = FALSE)

PE.results <- PanelEstimate(sets = PM.results_jun, data = data2)
#print results
PE.results2
#print plot
substplot <- plot(PE.results, 
                  main="Declared support party")

######################################################################
####################### FIGURE 3 in Paper ############################
######################################################################

par(mfrow=c(2,2))
plot(PE.results4, 
     main="Junior coalition partner")
plot(PE.results3, 
     main="Any kind of support party")
plot(PE.results2, 
     main="Contract support party")
plot(PE.results, 
     main="Declared support party")
mtext("Treatment effects for parties, Panel Match Approach",          
      font = 2,
      cex=1.5,
      side = 3,
      line = -1.5,
      outer = TRUE)

#=========================================================================#
###########################################################################
########## ANALYSIS SCANDINAVIA ONLY, Figures 8 & 9, Models 85-88 #########
###########################################################################
#=========================================================================#

# Read Stata data for Scandinavia only 
data1 <- read_dta(".../scandinavia_only_data_ThuerkKluever.dta")

# Create data frame with variables of interest 
data3 <- 	data.frame(
  year=data1$year,
  party=data1$party,
  treat_support=data1$treat_support,
  treat_support_formal=data1$treat_support_formal,
  treat_support_subst=data1$treat_support_subst,
  treat_junior=data1$treat_junior,
  next_pervote=data1$next_pervote,
  pervote=data1$pervote,
  e_id=data1$e_id
)
# Need to be integers 
data3$year <- as.integer(data1$year)
data3$party <- as.integer(data1$party)

######################################################################
####################### FIGURE 8 in Appendix #########################
######################################################################
### For junior coalition parties
DisplayTreatment(unit.id = "party",
                 time.id = "year", legend.position = "none",
                 xlab = "year", ylab = "Country Code",
                 treatment = "treat_junior", data = data3)

### For support parties (data added manually into upper graph)
DisplayTreatment(unit.id = "party",
                 time.id = "year", legend.position = "none",
                 xlab = "year", ylab = "Country Code",
                 treatment = "treat_support", data = data3)

######################################################################
####################### JUNIOR COALITION PARTY #######################
######################################################################
#### Model 85
PM.results_subs1 <- PanelMatch(lag = 1, time.id = "year", unit.id = "party", 
                              treatment = "treat_junior", refinement.method = "ps.match", 
                              data = data3, match.missing = TRUE, 
                              covs.formula = ~ I(lag(pervote, 1:2)), 
                              size.match = 3, qoi = "att" ,outcome.var = "next_pervote",
                              lead = 0:2, forbid.treatment.reversal = FALSE)
PE.results41 <- PanelEstimate(sets = PM.results_subs1, data = data3)
#print results
PE.results41
#print plot
juniorplot1 <- plot(PE.results41, 
                   main="Junior coalition partner")

######################################################################
####################### ANY KIND OF SUPPORT ##########################
######################################################################
#### Model 86
PM.results_supp1 <- PanelMatch(lag = 1, time.id = "year", unit.id = "party", 
                              treatment = "treat_support", refinement.method = "ps.match", 
                              data = data3, match.missing = TRUE, 
                              covs.formula = ~ I(lag(pervote, 1:2)), 
                              size.match = 3, qoi = "att" ,outcome.var = "next_pervote",
                              lead = 0:2, forbid.treatment.reversal = FALSE)
PE.results31 <- PanelEstimate(sets = PM.results_supp1, data = data3)
#print results
PE.results31
#print plot
supportplot1 <- plot(PE.results31, 
                    main="Any kind of support party")

######################################################################
####################### CONTRACT SUPPORT PARTY########################
######################################################################
#### Model 87
PM.results_formal1 <- PanelMatch(lag = 1, time.id = "year", unit.id = "party", 
                                treatment = "treat_support_formal", refinement.method = "ps.match", 
                                data = data3, match.missing = TRUE, 
                                covs.formula = ~ I(lag(pervote, 1:2)), 
                                size.match = 3, qoi = "att" ,outcome.var = "next_pervote",
                                lead = 0:2, forbid.treatment.reversal = FALSE)
PE.results21 <- PanelEstimate(sets = PM.results_formal1, data = data3)
#print results
PE.results21
#print plot
contractplot1 <- plot(PE.results21, 
                     main="Contract support party")

######################################################################
####################### DECLARED SUPPORT PARTY########################
######################################################################
#### Model 84
PM.results_jun1 <- PanelMatch(lag = 1, time.id = "year", unit.id = "party", 
                             treatment = "treat_support_subst", refinement.method = "ps.match", 
                             data = data3, match.missing = TRUE, 
                             covs.formula = ~ I(lag(pervote, 1:2)), 
                             size.match = 3, qoi = "att" ,outcome.var = "next_pervote",
                             lead = 0:2, forbid.treatment.reversal = FALSE)
PE.results1 <- PanelEstimate(sets = PM.results_jun1, data = data3)
#print results
PE.results1
#print plot
substplot1 <- plot(PE.results1, 
                  main="Declared support party")

######################################################################
####################### FIGURE 9 in Appendix #########################
######################################################################
par(mfrow=c(2,2))
plot(PE.results41, 
     main="Junior coalition partner")
plot(PE.results31, 
     main="Any kind of support party")
plot(PE.results21, 
     main="Contract support party")
plot(PE.results1, 
     main="Declared support party")
mtext("Treatment effects for parties, Panel Match Approach Scandinavia",                   
      font = 2,
      cex=1.5,
      side = 3,
      line = -1.5,
      outer = TRUE)


